﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for ActivityBO
    /// </summary>
    public class ActivityBO : DBConnectionBO
    {
        public ActivityBO():base()
        {
        }

        /// <summary>
        /// Get list Activity
        /// </summary>
        /// <returns></returns>
        public List<Activity> GetListActivity()
        {
            var listActivity = new List<Activity>();
            const string query = "SELECT TOP 100 * FROM [PrivateBlog].[dbo].[Activities]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            this.CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var activity = new Activity
                    {
                        Id = (int)dataTable.Rows[i][0],
                        Name = (string)dataTable.Rows[i][1],
                        IdUser = (int)dataTable.Rows[i][2],
                        IdObject = (string)dataTable.Rows[i][3],
                        Link = (string)dataTable.Rows[i][4],
                        LastDateUpdated = (DateTime)dataTable.Rows[i][5],
                        Status = Convert.ToInt32(dataTable.Rows[i][6])
                    };
                listActivity.Add(activity);
            }
            return listActivity;
        }

        /// <summary>
        /// Add new a activity
        /// </summary>
        /// <param name="activity"></param>
        public int AddActivity(Activity activity)
        {
            var query = "INSERT INTO [PrivateBlog].[dbo].[Activities]([Name],[IdUser],[IdObject],[Link],[LastDateUpdated],[Status]) VALUES("
                        + "N'"
                        + activity.Name + "', "
                        + activity.IdUser + ", N'"
                        + activity.IdObject + "', N'"
                        + activity.Link + "', "
                        + "GETDATE(), "
                        + activity.Status
                        + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a activity
        /// </summary>
        /// <param name="activity"></param>
        public int EditActivity(Activity activity)
        {
            var query = "UPDATE [PrivateBlog].[dbo].[Activities]" +
                        "SET NAME = N'" + activity.Name + "', " +
                        "IdUser = " + activity.IdUser + ", " +
                        "IdObject = '" + activity.IdObject + "', " +
                        "Link = N'" + activity.Link + "', " +
                        "LastDateUpdated = GETDATE(), " +
                        "Status = " + activity.Status +
                        "WHERE Id = " + activity.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a activity
        /// </summary>
        /// <param name="activity"></param>
        public int DeleteActivity(Activity activity)
        {
            var query = "DELETE FROM [PrivateBlog].[dbo].[Activities] " +
                        " WHERE Id = " + activity.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }
    }
}